package com.xuanheng.utils.http.async;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


public class AsyncHttpClient {

	private String userAgent="";
	private Map<String, List<Object>> headers = new HashMap<>();

	public void post(String url, RequestParams params, IResponseHandler handler){
		if(params==null){
			params=new RequestParams();
		}
		AsyncHttpRequest req=new AsyncHttpRequest(HttpMethod.POST, url, params, userAgent, headers, handler);
		req.execute();
	}
	public void get(String url, RequestParams params, boolean can302, IResponseHandler handler){
		if(params==null){
			params=new RequestParams();
		}
		AsyncHttpRequest req=new AsyncHttpRequest(HttpMethod.GET, url, params, userAgent, headers, handler);
		req.setCan302(can302);
		req.execute();
	}

	public void post(String url, int timeout, RequestParams params, IResponseHandler handler){
		if(params==null){
			params=new RequestParams();
		}
		AsyncHttpRequest req=new AsyncHttpRequest(HttpMethod.POST, url, params, userAgent, headers, handler);
		req.execute();
	}
	public void get(String url, int timeout, RequestParams params, IResponseHandler handler){
		if(params==null){
			params=new RequestParams();
		}
		AsyncHttpRequest req=new AsyncHttpRequest(HttpMethod.GET, url, params, userAgent, headers, handler);
		req.setTimeout(timeout);
		req.execute();
	}

	public void addHeader(String name, Object value) {
		List<Object> list = headers.get(name);
		if (list == null) {
			headers.put(name, list = new LinkedList<>());
		}
		list.add(value);
	}
	
	public void clearAllHeaders(){
		headers.clear();
	}
	public String getUserAgent() {
		return userAgent;
	}


	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
}
